/*计算两经纬度点之间的距离(单位：米)*/
// caculateLL(lat1, lng1, lat2, lng2) {
//     var radLat1 = lat1 * Math.PI / 180.0;
//     var radLat2 = lat2 * Math.PI / 180.0;
//     var a = radLat1 - radLat2;
//     var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
//     var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(
//       Math.sin(b / 2), 2)));
//     s = s * 6378.137;
//     s = Math.round(s * 10000) / 10;
//     return s;
//   },
export const caculateLL = function(lat1, lng1, lat2, lng2) {
  var radLat1 = lat1 * Math.PI / 180.0;
  var radLat2 = lat2 * Math.PI / 180.0;
  var a = radLat1 - radLat2;
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(
    Math.sin(b / 2), 2)));
  s = s * 6378.137;
  s = Math.round(s * 10000) / 10;
  return s;
}
/**
 * 函数防抖封装
 * @param {*} fn 回调函数
 * @param {*} wait  时间
 */
export const debounce = (fn, wait = 1000) => {
  let timer;
  return function() {
    let context = this;
    let args = arguments;
    timer && clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(context, args);
    }, wait);
  };
}
// 记录路由地址
export const fungoPreviousPage = () => {
  let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  let curRoute = routes[routes.length - 1].route //获取当前页面路由
  let curParam = routes[routes.length - 1].options; //获取路由参数
  // 拼接参数
  let param = ''
  for (let key in curParam) {
    if (!param) {
      param += key + '=' + curParam[key]
    } else {
      param += '&' + key + '=' + curParam[key]
    }
  }
  // 如果是首页则不记录
  if (curRoute != 'pages/home/home') {
    uni.setStorageSync('pageUrl', '/' + curRoute + '?' + param)
  }
}

// 获取pageUrl 如果有就跳转
export const goPage = () => {
  var pageUrl = uni.getStorageSync('pageUrl');
  if (pageUrl) {
    uni.reLaunch({
      url: pageUrl
    })
    //跳转后，删除url记录避免重复跳转
    uni.removeStorageSync('pageUrl');
    return;
  }
  uni.reLaunch({
    url: '/pages/home/home'
  })
}
